package com.cloudm.technician.ui.modules.cloudm_box.plate_number

import com.cloudm.technician.data.model.ScanResultInfo
import com.cloudm.technician.data.network.CloudmBoxApi
import com.cloudm.technician.extensions.logger
import com.cloudm.technician.ui.base.RxPresenter

/**
 * 作者: wesley
 * 时间: 2017/12/27
 * 描述: 业务逻辑
 */
class PlateNumberPresenter(private val view: PlateNumberContract.View,
                           private val cloudmBoxApi: CloudmBoxApi) : RxPresenter(),
        PlateNumberContract.Presenter {

    override fun getDataString(license: String?) {
        request(cloudmBoxApi.getDataString(license), view, onSuccess = { info, _ ->
            when (info) {
                null -> {
                    view.showLoadInfoEmpMsg()
                    view.finishSelf()
                }
                else -> {
                    view.initHeader(info.deviceDTO)
                    buildSingleTitle(info.singleList)
                    buildOilTitle(info.oilList)
                    buildSingleRowAndContent(info.singleList)
                    buildOilRowAndContent(info.oilList)
                }
            }
        }, onFailure = { errorMsg ->
            view.loadFailure(errorMsg)
            view.finishSelf()
        })
    }

    private fun buildSingleTitle(list: ArrayList<String>?) {
        if (list == null || list.size == 0) {
            logger(" buildSingleTitle list = $list")
            return
        }
        val listString = list[0].split(",")
        val listSingle = arrayListOf<ScanResultInfo>()
        (0 until listString.size).mapTo(listSingle) { ScanResultInfo(listString[it]) }
        view.singleTitleBuildSuccess(listSingle)
    }

    private fun buildOilTitle(list: ArrayList<String>?) {
        if (list == null || list.size == 0) {
            logger(" buildOilTitle list = $list")
            return
        }
        val listString = list[0].split(",")
        val listOil = arrayListOf<ScanResultInfo>()
        (0 until listString.size).mapTo(listOil) { ScanResultInfo(listString[it]) }
        view.OilTitleBuildSuccess(listOil)
    }

    private fun buildSingleRowAndContent(list: ArrayList<String>?) {
        if (list == null || list.size < 1) {
            logger(" buildSingleRowAndContent list = $list")
            return
        }
        val listRow = arrayListOf<ScanResultInfo>()
        val listContent: ArrayList<ArrayList<String?>> = arrayListOf()
        for (i in 1 until list.size) {
            val listString = list[i].split(",")
            for(j in 0 until listString.size){
                when(j){
                    0 -> listRow.add(ScanResultInfo(listString[j]))
                }
            }
            val listItem = arrayListOf<String?>()
            (1 until listString.size).mapTo(listItem) { listString[it] }
            listContent.add(listItem)
        }
        view.buildSingRowAndContentSuccess(listRow, listContent)
    }

    private fun buildOilRowAndContent(list: ArrayList<String>?){
        if (list == null || list.size < 1) {
            logger(" buildOilRowAndContent list = $list")
            return
        }
        val listRow = arrayListOf<ScanResultInfo>()
        val listContent: ArrayList<ArrayList<String?>> = arrayListOf()
        for (i in 1 until list.size) {
            val listString = list[i].split(",")
            for(j in 0 until listString.size){
                when(j){
                    0 -> listRow.add(ScanResultInfo(listString[j]))
                }
            }
            val listItem = arrayListOf<String?>()
            (1 until listString.size).mapTo(listItem) { listString[it] }
            listContent.add(listItem)
        }
        view.buildOilRowAndContentSuccess(listRow, listContent)
    }

}